package com.gitee.kamismile.stone.rpc.core.provider.producer.assembly;

import com.gitee.kamismile.stone.rpc.api.engine.StoneAppEngine;
import com.gitee.kamismile.stone.rpc.common.support.StoneURL;
import com.gitee.kamismile.stone.rpc.core.invocation.StoneInvokeInvoker;
import com.gitee.kamismile.stone.rpc.core.proxy.IStoneProxy;
import com.gitee.kamismile.stone.rpc.core.proxy.imp.jdk.JDKProxy;
import org.springframework.beans.factory.FactoryBean;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.context.ApplicationContext;
import org.springframework.util.Assert;

public class StonePojoReferenceMeta implements FactoryBean<Object>, InitializingBean {

    private Class<?> mapperInterface;

    private String serverName;

    private String serverVersion;

    private int timeout;

    private Object proxy;

    private int retry;

    private StoneURL stoneURL;

    private StoneAppEngine stoneAppEngine;

    private ApplicationContext applicationContext;

    public Object getProxy() {
        return getObject();
    }

    @Override
    public Object getObject() {
        return proxy;
    }

    @Override
    public Class<?> getObjectType() {
        return mapperInterface;
    }

    @Override
    public void afterPropertiesSet() {
        Assert.notNull(mapperInterface, "mapperInterface is null");
        StoneInvokeInvoker stoneInvoker = new StoneInvokeInvoker(mapperInterface, serverName, serverVersion, stoneURL, timeout, stoneAppEngine);
        IStoneProxy stoneProxy = applicationContext.getBean(IStoneProxy.class);
        proxy = stoneProxy.getProxy(stoneInvoker);
    }

    @Override
    public boolean isSingleton() {
        return true;
    }

    public StoneURL getStoneURL() {
        return stoneURL;
    }

    public void setStoneURL(StoneURL stoneURL) {
        this.stoneURL = stoneURL;
    }

    public Class<?> getMapperInterface() {
        return mapperInterface;
    }

    public void setMapperInterface(Class<?> mapperInterface) {
        this.mapperInterface = mapperInterface;
    }

    public int getTimeout() {
        return timeout;
    }

    public void setTimeout(int timeout) {
        this.timeout = timeout;
    }

    public String getServerName() {
        return serverName;
    }

    public void setServerName(String serverName) {
        this.serverName = serverName;
    }

    public String getServerVersion() {
        return serverVersion;
    }

    public void setServerVersion(String serverVersion) {
        this.serverVersion = serverVersion;
    }

    public StoneAppEngine getStoneAppEngine() {
        return stoneAppEngine;
    }

    public void setStoneAppEngine(StoneAppEngine stoneAppEngine) {
        this.stoneAppEngine = stoneAppEngine;
    }

    public ApplicationContext getApplicationContext() {
        return applicationContext;
    }

    public void setApplicationContext(ApplicationContext applicationContext) {
        this.applicationContext = applicationContext;
    }

    public int getRetry() {
        return retry;
    }

    public void setRetry(int retry) {
        this.retry = retry;
    }
}
